 /*
 * 文 件 名:  DictionaryDAOImpl.java
 * 版    权:  Tydic Technologies Co., Ltd. Copyright 1993-2012,  All rights reserved
 * 描    述:  字典表操作
 * 修 改 人:  aganzhoul
 * 修改时间:  2012-9-20
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package com.tydic.simple.dao.user.impl;

import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import com.tydic.framework.dao.AbstractIbatisDAO;
import com.tydic.framework.dataobject.ResultDTO;
import com.tydic.framework.exception.DAOException;
import com.tydic.simple.dao.user.IDictionaryDAO;
import com.tydic.simple.dto.user.DicDTO;
import com.tydic.simple.model.user.Dictionary;

 /**
 * 字典表操作
 * 
 * @author  aganzhoul
*  @version  [1.0, 2012-9-20]
*  @since  [权限管理/1.0]
 */
public class DictionaryDAOImpl extends AbstractIbatisDAO implements IDictionaryDAO {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(DictionaryDAOImpl.class);
    
    /** {@inheritDoc} */
    @Override
    public Long saveDictionary(Dictionary dictionary)
        throws DAOException {
        try {
            sqlMapClient.insert("user.INSERT_DICTIONARY", dictionary);
        }
        catch (SQLException e) {
            LOGGER.error("saveDictionary exception",e);
            throw new DAOException("saveDictionary exception", e);
        }
        return dictionary.getDicId();
    }
    
    /** {@inheritDoc} */
    @Override
    public int deleteDicByCode(String dicCode)
        throws DAOException {
        try {
            return sqlMapClient.delete("user.DELETE_DIC_BY_CODE", dicCode);
        }
        catch (SQLException e) {
            LOGGER.error("deleteDicByCode exception",e);
            throw new DAOException("deleteDicByCode exception", e);
        }
    }
    
    /** {@inheritDoc} */
    @Override
    public Dictionary findDicByCode(String dicCode)
        throws DAOException {
        try {
            return (Dictionary)sqlMapClient.queryForObject("user.FIND_DIC_BY_CODE", dicCode);
        }
        catch (SQLException e) {
            LOGGER.error("findDicByCode exception",e);
            throw new DAOException("findDicByCode exception", e);
        }
    }
    
    /** {@inheritDoc} */
    @SuppressWarnings("unchecked")
    @Override
    public ResultDTO<Dictionary> findDictionary(DicDTO dicDTO)
        throws DAOException {
        ResultDTO<Dictionary> result = new ResultDTO<Dictionary>();
        try {
            result.setResult(sqlMapClient.queryForList("user.FIND_DICTIONARY", dicDTO));
            BeanUtils.copyProperties(dicDTO.getPagination(), result.getPagination());
            result.getPagination().setTotal((Integer)sqlMapClient.queryForObject("user.FIND_DIC_COUNT", dicDTO));
        }
        catch (SQLException e) {
            LOGGER.error("findDictionary exception",e);
            throw new DAOException("findDictionary exception", e);
        }
        return result;
    }
    
}
